package com.android.signhelper.db;

import android.content.ContentValues;
import android.content.pm.SigningInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;


import com.android.signhelper.util.MyApplication;
import com.android.signhelper.bean.SignInfo;
import com.android.signhelper.bean.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ikata on 2018/11/14.
 */

public class MyDatabase {
    public static final String TABLE_STUDENT = "student";
    public static final String TABLE_SIGNINFO = "signinfo";

    private MyDatabaseHelper databaseHelper;
    public MyDatabase(){
        databaseHelper = new MyDatabaseHelper(MyApplication.getContext(), "StudentStore.db", null, 5);
    }

    /**
     * 基础数据 username password token name
     * @param student
     */
    public void insertStudent(Student student)
    {
        SQLiteDatabase database=databaseHelper.getWritableDatabase();
        Cursor cursor = database.query(TABLE_STUDENT, null, "userName = ?", new String[]{student.getUserName()}, null, null, null, null);
        if (cursor.getCount()>0){
            cursor.close();
            database.close();
            return;
        }
        ContentValues values=new ContentValues();
        values.put("userName",student.getUserName());
        values.put("password",student.getPassword());
        values.put("token",student.getToken());
        values.put("name",student.getName());
        if (student.getSignInfo()!=null) {
            values.put("signinfoId", student.getSignInfo().getDevUuid());
        }
        database.insert(TABLE_STUDENT, null, values);
        database.close();
    }

    public String getUserName(String token)
    {
        String userName = null;
        SQLiteDatabase database = databaseHelper.getReadableDatabase();
        Cursor cursor=database.query(TABLE_STUDENT, new String[]{"userName"}, "token = ?", new String[]{token}, null, null, null);
        if (cursor.getCount()>0){
            cursor.moveToFirst();
            userName = cursor.getString(cursor.getColumnIndex("userName"));

        }
        cursor.close();
        database.close();
        return userName;
    }

    public boolean insertSignInfo(SignInfo signInfo)
    {
        if (signInfo==null||signInfo.getDevUuid()==null){
            return false;
        }
        SQLiteDatabase database=databaseHelper.getWritableDatabase();
        Cursor cursor = database.query(TABLE_SIGNINFO, null, "devUuid = ?", new String[]{signInfo.getDevUuid()}, null, null, null);
        if (cursor.getCount()>0){
            updateSignInfo(signInfo);
            return true;
        }
        ContentValues values=new ContentValues();
        values.put("devUuid",signInfo.getDevUuid());
        values.put("bleId",signInfo.getBleId());
        values.put("signId",signInfo.getSignId());
        values.put("available",signInfo.isAvailable());
        long result=database.insert(TABLE_SIGNINFO, null, values);
        cursor.close();
        database.close();
        return result != -1;
    }

    public void updateStudent(Student student)
    {
        SQLiteDatabase database=databaseHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("name",student.getName());
        values.put("message",student.getMessage());
        values.put("token",student.getToken());
        values.put("message",student.getMessage());
        database.update(TABLE_STUDENT, values, "userName = ?", new String[]{student.getUserName()});
        database.close();
    }

    public void updateStudent(String userName,ContentValues contentValues){
        SQLiteDatabase database = databaseHelper.getWritableDatabase();
        database.update(TABLE_STUDENT, contentValues, "userName = ?", new String[]{userName});
        database.close();
    }

    public void bindInfo(String userName,String signinfoId)
    {
        SQLiteDatabase database=databaseHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("signinfoId",signinfoId);
        database.update(TABLE_STUDENT, values, "userName = ?", new String[]{userName});
        database.close();
    }

    public void updateSignInfo(SignInfo signInfo)
    {
        if (signInfo == null) {
            return;
        }
        SQLiteDatabase database=databaseHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("available",signInfo.isAvailable());
        values.put("bleId",signInfo.getBleId());
        values.put("signId",signInfo.getSignId());
        database.update(TABLE_SIGNINFO,values,"devUuid = ?",new String[]{signInfo.getDevUuid()});
        database.close();

    }

    public List<Student> queryAll()
    {
        List<Student> list = new ArrayList<>();
        SQLiteDatabase database=databaseHelper.getReadableDatabase();
        Cursor cursor = database.query(TABLE_STUDENT,null,null,null,null,null,null);
        if (cursor.moveToFirst()) {
            do {
                Student student = new Student();
                student.setUserName(cursor.getString(cursor.getColumnIndex("userName")));
                student.setName(cursor.getString(cursor.getColumnIndex("name")));
                student.setPassword(cursor.getString(cursor.getColumnIndex("password")));
                student.setMessage(cursor.getString(cursor.getColumnIndex("message")));
                student.setSignInfo(querySignInfo(cursor.getString(cursor.getColumnIndex("signinfoId"))));
                student.setToken(cursor.getString(cursor.getColumnIndex("token")));
                list.add(student);
            }
            while (cursor.moveToNext());
            cursor.close();
        }
        database.close();
        return list;
    }

    public SignInfo querySignInfo(String devUuid)
    {
        if (TextUtils.isEmpty(devUuid))
        {
            return null;
        }
        SQLiteDatabase database=databaseHelper.getReadableDatabase();
        Cursor cursor= database.query(TABLE_SIGNINFO, null, "devUuid = ?", new String[]{devUuid}, null, null, null);
        if (cursor.getCount()==0){
            return null;
        }
        SignInfo signInfo = new SignInfo();
        if (cursor.moveToFirst()) {
            signInfo.setAvailable(Integer.parseInt(cursor.getString(cursor.getColumnIndex("available"))));
            signInfo.setBleId(cursor.getString(cursor.getColumnIndex("bleId")));
            signInfo.setDevUuid(cursor.getString(cursor.getColumnIndex("devUuid")));
            signInfo.setSignId(cursor.getString(cursor.getColumnIndex("signId")));
            cursor.close();
        }
        database.close();
        return signInfo;
    }


    @Nullable
    public Student queryStudent(String token)
    {
        SQLiteDatabase database = databaseHelper.getReadableDatabase();
        Cursor cursor=database.query(TABLE_STUDENT,null,"token = ?",new String[]{token},null,null,null);
        if (cursor.getCount()==0){
            return null;
        }
        cursor.moveToFirst();
        String devUuid = cursor.getString(cursor.getColumnIndex("signinfoId"));
        SignInfo signingInfo = querySignInfo(devUuid);
        Student student=new Student(
                cursor.getString(cursor.getColumnIndex("name")),
                cursor.getString(cursor.getColumnIndex("userName")),
                cursor.getString(cursor.getColumnIndex("password")),
                signingInfo,
                token,
                cursor.getString(cursor.getColumnIndex("message")));
        cursor.close();
        database.close();
        return student;
    }


    public void updateMsg(String msg,String userName)
    {
        SQLiteDatabase database=databaseHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("message",msg);
        database.update(TABLE_STUDENT, values, "userName = ?", new String[]{userName});
        database.close();
    }
}
